package com.fr.lintcode;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

/**
*作者：furong
*日期：2016年12月22日
*时间：下午5:17:30
*/
public class Q55 {
    public boolean compareStrings(String a, String b) {
        if (a == null || b == null) {
            return false;
        }
        Map<Character, Integer> aa = buildMap(a);
        Map<Character, Integer> bb = buildMap(b);
        for (java.util.Map.Entry<Character, Integer> e : bb.entrySet()) {
            char c = e.getKey();
            int i = e.getValue();
            Integer ai = aa.get(c);
            if (ai == null || ai < i) {
                return false;
            }
        }
        return true;
    }

    private Map<Character, Integer> buildMap(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Integer j = map.get(c);
            if (j == null) {
                map.put(c, 1);
            } else {
                j++;
                map.put(c, j);
            }

        }
        return map;
    }

    @Test
    public void anagram() {
        String s = "ab";
        String t = "ab";
        System.out.println(compareStrings(s, t));
    }
}
